package com.wangdong.mijiqiqi.downloadthhreaddb;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.wangdong.mijiqiqi.downloadthhreaddb.service.DownloadService;
import com.wangdong.mijiqiqi.R;


public class DownloadActivity extends AppCompatActivity implements View.OnClickListener{
    private TextView fileName;
    private Button startButton;
    private Button stopButton;
    private ProgressBar downloadProgress;
    private String urlstr = "http://aiwolvju.b0.upaiyun.com/uploads/20181030/jy2xma7a7itdcwgmrk57nlh6priqeryu.tmp";
    private FileInfo fileInfo;
    private Intent intent;
    private UIRecive mRecive;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download);

        // 初始化控件
        fileName = (TextView) findViewById(R.id.file_textview);//显示下载文件名字
        downloadProgress = (ProgressBar) findViewById(R.id.progressBar2);//进度条显示进度
        startButton = (Button) findViewById(R.id.start_button);//开始下载点击按钮
        stopButton = (Button) findViewById(R.id.stop_button);//停止下载暂停下载按钮
        downloadProgress.setMax(100);//进度条下载最大为100
        // 初始化文件对象    id设置为0  下载路径    文件名字            开始文件大小   结束文件大小
        fileInfo = new FileInfo(0, urlstr, getfileName(urlstr), 0, 0);
        //设置点击监听
        startButton.setOnClickListener(this);
        stopButton.setOnClickListener(this);
        //创建意图,是个服务意图
        intent = new Intent(this, DownloadService.class);
        //创建广播, 接受广播的
        mRecive = new UIRecive();
        //创建广播接受者
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(DownloadService.ACTION_UPDATE);
        registerReceiver(mRecive, intentFilter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start_button:
                // 开启服务
                fileName.setText(getfileName(urlstr));//设置显示下载文件名字
                intent.setAction(DownloadService.ACTION_START);
                intent.putExtra("fileInfo", fileInfo);
                startService(intent);

                break;
            case R.id.stop_button:
                intent.setAction(DownloadService.ACTION_STOP);
                intent.putExtra("fileInfo", fileInfo);
                startService(intent);
                break;
        }

    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(mRecive);//注销广播
        super.onDestroy();
    }
    // 從URL地址中獲取文件名，即/後面的字符
    private String getfileName(String url) {

//        return urlstr.substring(urlstr.lastIndexOf("/") + 1);
        return "wangdong.apk";
    }

    // 從DownloadTadk中獲取廣播信息，更新進度條
    class UIRecive extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (DownloadService.ACTION_UPDATE.equals(intent.getAction())) {
                int finished = intent.getIntExtra("finished", 0);
                downloadProgress.setProgress(finished);

            }
        }

    }

}
